const autoprefixer = require('autoprefixer')
// const pxtorem = require('postcss-pxtorem')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const path = require('path')

console.info(process.env.NODE_ENV)

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  lintOnSave: false,
  productionSourceMap: process.env.NODE_ENV === 'development' ? true : false, // 去掉打包后js中，会自动生成的一些map文件
  devServer: {
    host: "0.0.0.0",
    port: 8888, // 端口
    proxy: {
      '/api': {
        // target: 'http://wx.lohike.net',
        target: 'http://wx-mini-shop.dev.com',
        changeOrigin: true, // target是域名的话，需要这个参数，
        secure: false // 设置支持https协议的代理
        // pathRewrite: { '^/api': '' }
      },
      '/shop_api': {
        target: 'https://api.it120.cc/xiaochengxu',
        changeOrigin: true, // target是域名的话，需要这个参数，
        secure: false, // 设置支持https协议的代理
        pathRewrite: { '^/shop_api': '' }
      }
    }
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
      .set('@styles', resolve('src/styles'))
  },
  configureWebpack: config => {
    config.plugins.push(new CompressionWebpackPlugin({
      // 目标文件名称。[path] 被替换为原始文件的路径和 [query] 查询
      // filename: '[path].gz[query]',
      // 使用 gzip 压缩
      algorithm: 'gzip',
      // 处理与此正则相匹配的所有文件
      test: new RegExp(
        '\\.(js|css)$'
      ),
      // 只处理大于此大小的文件
      threshold: 10240,
      // 最小压缩比达到 0.8 时才会被压缩
      minRatio: 0.8,
    }));
    // 开启分离js
    config.optimization = {
      runtimeChunk: 'single',
      splitChunks: {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 20000,
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name(module) {
              // get the name. E.g. node_modules/packageName/not/this/part.js
              // or node_modules/packageName
              const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
              // npm package names are URL-safe, but some servers don't like @ symbols
              return `npm.${packageName.replace('@', '')}`
            }
          }
        }
      }
    };
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer({
            overrideBrowserslist: ['Android >= 4.0', 'iOS >= 7']
          }),
          // pxtorem({
          //   rootValue: 50,
          //   unitPrecision: 3,
          //   propList: ['*', '!font*'],
          //   selectorBlackList: ['.ignore ', '.hairlines', 'van-circle__layer', '.van-hairline'],
          //   minPixelValue: 2
          // })
        ]
      }
    }
  }
}